import debounce from 'lodash/debounce'

export default {
  data () {
    return {
      yuanmei_lists_acculate_columns: [],

      // 作为标识当前是表1-2或表1-2-1
      maybe_i_want_to_use_yuanmei: true,

      // 存储查询出来的原煤采用指标系数
      origin_user_yuanmei_factor: '',
    }
  },
  computed: {
    wuyanmei () {
      return this.lists.find(v => (v.data.find(vv => vv.key === 'code') || {}).value === '02');
    },
    lianjiaoyanmei () {
      return this.lists.find(v => (v.data.find(vv => vv.key === 'code') || {}).value === '03')
    },
    yibanyanmei () {
      return this.lists.find(v => (v.data.find(vv => vv.key === 'code') || {}).value === '04')
    },
    hemei () {
      return this.lists.find(v => (v.data.find(vv => vv.key === 'code') || {}).value === '05')
    },
    useQizhong () {
      const keys = this.yuanmei_lists_acculate_columns;
      const hasValue = (data) => data.some(v => keys.includes(v.key) && v.value !== '')
      return hasValue(this.wuyanmei ? (this.wuyanmei.data || []) : []) ||
        hasValue(this.lianjiaoyanmei ? (this.lianjiaoyanmei.data || []) : []) ||
        hasValue(this.yibanyanmei ? (this.yibanyanmei.data || []) : []) ||
        hasValue(this.hemei ? (this.hemei.data || []) : [])
    },
  },
  watch: {
    wuyanmei: {
      deep: true,
      immediate: true,
      handler () {
       this.yuanmei_qizhongxiang_watch_hander()
      },
    },
    lianjiaoyanmei: {
      deep: true,
      immediate: true,
      handler () {
       this.yuanmei_qizhongxiang_watch_hander()
      },
    },
    yibanyanmei: {
      deep: true,
      immediate: true,
      handler () {
       this.yuanmei_qizhongxiang_watch_hander()
      },
    },
    hemei: {
      deep: true,
      immediate: true,
      handler () {
       this.yuanmei_qizhongxiang_watch_hander()
      },
    },
    // 原煤子项填写了，则“原煤”不可编辑
    useQizhong: {
      immediate: true,
      handler (val) {
        const yuanmei = this.lists.find(v => (v.data.find(vv => vv.key === 'code') || {}).value === '01')
        if (yuanmei) {
          const toggleEditable = (key) => {
            const data = yuanmei.data;
            if (data) {
              const target = data.find(v => v.key === key);
              if (target) {
                target.editable = !val;
                target._computed = val;
              }
            }
          }
          (this.yuanmei_lists_acculate_columns_all || []).forEach(toggleEditable);
        }
      }
    },
  },
  methods: {
    yuanmei_qizhongxiang_watch_hander: debounce(function yuanmei_qizhongxiang_watch_hander () {
      const lists = this.lists;
      if (lists && lists.length > 0) {
        // ['无烟煤', '炼焦烟煤', '一般烟煤', '褐煤'];
        const wuyanmei = this.wuyanmei || {};
        const lianjiaoyanmei = this.lianjiaoyanmei || {};
        const yibanyanmei = this.yibanyanmei || {};
        const hemei = this.hemei || {};
        const yuanmei = lists.find(v => (v.data.find(vv => vv.key === 'code') || {}).value === '01');
        const acculate = (key) => {
          const x = (wuyanmei.data || []).find(v => v.key === key);
          const y = (lianjiaoyanmei.data || []).find(v => v.key === key)
          const p = (yibanyanmei.data || []).find(v => v.key === key)
          const q = (hemei.data || []).find(v => v.key === key)
          const vx = x ? Number(x.value) || 0 : 0;
          const vy = x ? Number(y.value) || 0 : 0;
          const vp = x ? Number(p.value) || 0 : 0;
          const vq = x ? Number(q.value) || 0 : 0;

          return vx + vy + vp + vq;
        }
        const update = (key, value) => {
          if (yuanmei) {
            const data = yuanmei.data || [];
            const d = data.find(v => v.key === key)
            if (d) {
              d.value = value != null ? value : acculate(key)
            }
          }
        }

        if (!this.useQizhong) {
          update('useScalingFactor', this.origin_user_yuanmei_factor);
          return;
        }

        this.yuanmei_lists_acculate_columns.forEach(v => update(v));

        // 表1-2的原煤采用折标系数也要根据所填原煤子项计算出来
        // z = (∑原煤子项工业生产消费*折标系数)/∑原煤子项工业生产消费
        // industrialProduction, useScalingFactor
        if (this.i_am_t_1_2) {
          const lists = this.lists.filter(v => this.isQiZhong(v.data));
          if (lists.length < 1) {
            update('useScalingFactor', this.origin_user_yuanmei_factor);
            return;
          }

          const s = lists.map(v => {
            const x = (v.data || []).find(v => v.key === 'industrialProduction') || {};
            const y = (v.data || []).find(v => v.key === 'useScalingFactor') || {};
            return [Number(x.value) || 0, Number(y.value) || 0]
          });
          const acc_industrial_production = () => {
            return s.reduce((acc, cur) => {
              acc += cur[0];
              return acc;
            }, 0)
          };
          const acc_industrial_production_with_factor = () => {
            return s.reduce((acc, cur) => {
              acc += cur[0] * cur[1]
              return acc;
            }, 0)
          };

          try {
            const p = acc_industrial_production_with_factor();
            const q = acc_industrial_production();

            if (q === 0) {
              update('useScalingFactor', this.origin_user_yuanmei_factor);
              return
            }

            const z = p / q;
            update('useScalingFactor', z ? z.toFixed(this.precision || 4) : this.origin_user_yuanmei_factor);
          } catch (e) {}
        }
      }
    }, 500),
    prefixBlankWithQizhong (val) {
      if (val === '无烟煤') return '其中：1.';
      if (val === '炼焦烟煤') return '2.';
      if (val === '一般烟煤') return '3.';
      if (val === '褐煤') return '4.';
      return '';
    },
    isQiZhong (data = []) {
      const c = data.find(v => v.key === 'code')
      return c && ['02', '03', '04', '05'].includes(c.value)
    }
  },
}
